package com.seo.controller.menu;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seo.pojo.InputBody;
import com.seo.pojo.ReturnBody;
import com.seo.resolver.DefiniteException;
import com.seo.service.sysMenu.sdk.SysMenuService;
import com.seo.service.sysRole.sdk.SysRoleService;
import com.seo.utils.Const;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

/**
 * menucontroller <br/>
 * 
 * @author shibeilei
 * @version 2017.11.30 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/menu")
public class MenuController {

	@Autowired
	private SysMenuService sysMenuService;

	@Autowired
	private SysRoleService sysRoleService;

	private String menuUrl = "menu";

	@RequestMapping
	public String list(Integer parentId, HttpServletRequest request, HttpSession session) {
		log.info("进入菜单...");
		if (!Jurisdiction.qx(menuUrl)) {
			throw new DefiniteException("权限不足");
		}

		if (parentId == null) {
			parentId = 0;
		}
		if (parentId != 0) {
			Map<String, Object> pMenu = sysMenuService.getMenuById(parentId);
			if (pMenu == null) {
				throw new DefiniteException("父级菜单不存在");
			}
			request.setAttribute("pMenu", pMenu);
		}
		List<Map<String, Object>> menuList = sysMenuService.getMenuByParentId(parentId);
		request.setAttribute("parentId", parentId);
		request.setAttribute("menuList", menuList);
		return "menu/list";

	}

	@RequestMapping("/getList")
	@ResponseBody
	public ReturnBody getList(Integer parentId, HttpServletRequest request, HttpSession session) {
		log.info("获取菜单...");
		ReturnBody body = ReturnBody.init();
		if (!Jurisdiction.qx(menuUrl)) {
			return body.error("error", "权限不足");
		}

		if (parentId == null) {
			parentId = 0;
		}
		List<Map<String, Object>> menuList = sysMenuService.getMenuByParentId(parentId);
		body.put("menuList", menuList);
		return body.success();

	}

	@RequestMapping("/userList")
	@ResponseBody
	public ReturnBody userList(HttpServletRequest request, HttpSession session) {
		log.info("获取菜单...");
		ReturnBody rb = ReturnBody.init();

		Integer roleId = Jurisdiction.getT("roleId", Integer.class);
		String userType = Jurisdiction.getT("userType", String.class);
		if (session.getAttribute(Const.SESSION_MENULIST) == null) {
			rb.run(sysRoleService.getUserRoleMenu(userType, roleId));
			if (!rb.isSuccess()) {
				return rb.error();
			}
			session.setAttribute(Const.SESSION_MENULIST, rb.get("menuList", List.class));
			session.setAttribute(Const.SESSION_ROLE, rb.get("role", Map.class));
		} else {
			rb.put("menuList", session.getAttribute(Const.SESSION_MENULIST));
		}
		return rb.success();

	}

	@RequestMapping("/add")
	public String add(Integer parentId, HttpServletRequest request, HttpSession session) {
		log.info("去添加菜单页...");
		if (!Jurisdiction.qx(menuUrl, "add")) {
			throw new DefiniteException("权限不足");
		}
		if (parentId == null) {
			parentId = 0;
		}
		if (parentId != 0) {
			Map<String, Object> pMenu = sysMenuService.getMenuById(parentId);
			if (pMenu == null) {
				throw new DefiniteException("父级菜单不存在");
			}
			request.setAttribute("pMenu", pMenu);
		}
		request.setAttribute("action", "saveMenu");
		request.setAttribute("parentId", parentId);
		return "menu/edit";

	}

	@RequestMapping("/edit")
	public String edit(Integer id, HttpServletRequest request, HttpSession session) {
		log.info("去编辑菜单页...");
		if (!Jurisdiction.qx(menuUrl, "edit")) {
			throw new DefiniteException("权限不足");
		}
		if (id == null) {
			throw new DefiniteException("参数不能为空");
		}
		Map<String, Object> menu = sysMenuService.getMenuById(id);
		if (menu == null) {
			throw new DefiniteException("菜单不存在");
		}
		Integer parentId = (Integer) menu.get("parentId");
		if (parentId != 0) {
			Map<String, Object> pMenu = sysMenuService.getMenuById(parentId);
			if (pMenu == null) {
				throw new DefiniteException("父级菜单不存在");
			}
			request.setAttribute("pMenu", pMenu);
		}

		request.setAttribute("action", "editMenu");
		request.setAttribute("menu", menu);
		request.setAttribute("parentId", parentId);
		return "menu/edit";

	}

	@RequestMapping("/editMenu")
	public String editMenu() {
		log.info("保存菜单...");
		if (!Jurisdiction.qx(menuUrl, "edit")) {
			throw new DefiniteException("权限不足");
		}
		InputBody body = InputBody.init();
		log.info("参数-{}", body.getParams());
		ReturnBody rb = sysMenuService.updateMenu(body);
		if (!rb.isSuccess()) {
			throw new DefiniteException("保存失败");
		}
		String parentId = body.get("parentId", String.class);
		return "redirect:/menu?parentId=" + parentId;

	}

	@RequestMapping("/saveMenu")
	public String saveMenu() {
		log.info("保存菜单...");
		if (!Jurisdiction.qx(menuUrl, "edit")) {
			throw new DefiniteException("权限不足");
		}
		InputBody body = InputBody.init();
		log.info("参数-{}", body.getParams());
		ReturnBody rb = sysMenuService.saveMenu(body);
		if (!rb.isSuccess()) {
			throw new DefiniteException("保存失败");
		}
		String parentId = body.get("parentId", String.class);
		return "redirect:/menu?parentId=" + parentId;

	}

	@RequestMapping("/deleteMenu")
	@ResponseBody
	public ReturnBody deleteMenu(Integer id) {
		log.info("删除菜单...");
		ReturnBody rb = ReturnBody.init();
		if (!Jurisdiction.qx(menuUrl, "edit")) {
			return rb.error("error", "权限不足");
		}
		if (id == null || id <= 0) {
			return rb.error("error", "参数不能为空");
		}
		return sysMenuService.deleteMenu(id);
	}

	@RequestMapping("/getIcon")
	public String getIcon() {
		return "/menu/icon_list";
	}
}
